package com.jy.datapipeline.importserver.controller;

import com.jy.datapipeline.common.support.EasyExcelSupport;
import com.jy.datapipeline.common.support.ExcelSupport;
import com.jy.datapipeline.common.constants.ExcelConstants;
import com.jy.datapipeline.common.constants.FormatConstants;
import com.jy.datapipeline.importserver.common.dto.RuleTemplateDto;
import com.jy.datapipeline.common.exception.GlobalException;
import com.jy.datapipeline.common.util.DateTimeUtil;
import com.jy.datapipeline.common.vo.ReturnVo;
import com.jy.datapipeline.importserver.entity.ImportRule;
import com.jy.datapipeline.importserver.service.ImportRuleService;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.List;

/**
 * @类名 ImportRuleController
 * @描述 导入规则API
 * @作者 白条君
 * @创建日期 2023/4/16 23:16
 * @版本 1.0
 */
@RestController
@RequestMapping("/rule")
public class ImportRuleController {

    private final ImportRuleService importRuleService;

    private final ExcelSupport excelSupport;

    private final EasyExcelSupport easyExcelSupport;

    public ImportRuleController(ImportRuleService importRuleService, ExcelSupport excelSupport, EasyExcelSupport easyExcelSupport) {
        this.importRuleService = importRuleService;
        this.excelSupport = excelSupport;
        this.easyExcelSupport = easyExcelSupport;
    }

    @GetMapping("/query-by-page")
    public ReturnVo queryByPage(@RequestParam(defaultValue = "1") Long pageNo
            , @RequestParam(defaultValue = "10") Long pageSize
            , String ruleId, String sourceDatabase, String targetDatabaseId) {
        return importRuleService.queryByPage(pageNo, pageSize, ruleId, sourceDatabase, targetDatabaseId);
    }

    @GetMapping("/query-names")
    public ReturnVo queryNames(@RequestParam(required = false) String ruleName) {
        List<ImportRule> importRules = importRuleService.queryAllNames(ruleName);
        return ReturnVo.ok().put(importRules);
    }

    @PostMapping("/addition")
    public ReturnVo add(@RequestBody ImportRule importRule) throws GlobalException {
        importRuleService.addImportRule(importRule);
        return ReturnVo.ok();
    }

    @PutMapping("/modification")
    public ReturnVo modify(@RequestBody ImportRule importRule) throws GlobalException {
        importRuleService.modifyRule(importRule);
        return ReturnVo.ok();
    }

    @DeleteMapping("/deletion")
    public ReturnVo deletion(@RequestParam Long id) throws GlobalException, SchedulerException {
        importRuleService.deleteRule(id);
        return ReturnVo.ok();
    }

    @GetMapping("/download")
    public void downloadRuleTemplate(HttpServletResponse res) throws IOException {
        excelSupport.downloadTemplate(res, ExcelConstants.IMPORT_RULE_TEMPLATE_COLUMNS,
                ExcelConstants.IMPORT_RULE_TEMPLATE_COMMENTS,
                ExcelConstants.IMPORT_RULE_TEMPLATE_SHEET_NAME,
                ExcelConstants.IMPORT_RULE_TEMPLATE_EXAMPLE,
                ExcelConstants.IMPORT_RULE_TEMPLATE_NAME);
    }

    @PostMapping("/upload")
    public ReturnVo updateRuleTemplate(@RequestBody MultipartFile file) throws IOException {
        easyExcelSupport.analysisTemplate(file.getInputStream(), 10, importRuleService, RuleTemplateDto.class);
        return ReturnVo.ok();
    }
}
